/*******************************************************************************
 * Copyright (c) 2009-2011 ETCORE Labs. All rights reserved. 
 * 
 * This software is the confidential and proprietary information of ETCORE Labs.
 * You shall not disclose such confidential information and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with ETCORE Labs. 
 * 
 * Support can be obtained from support forums at:  http://www.etcore.com/
 * /
 *******************************************************************************/
package etcore.webapp.grouptalk.common.service;

import java.util.List;

import org.hibernate.Session;

import etcore.webapp.grouptalk.common.dao.CommentDAO;
import etcore.webapp.grouptalk.common.dao.HibernateSessionFactory;
import etcore.webapp.grouptalk.common.object.Comment;

/**
 * CommentService implements queries and transactions on Comment
 * 
 * @author ETCORE GroupTalk Developers
 * 
 */
public class CommentService extends BaseService {
	/**
	 * Find Comment instances
	 * 
	 * @param begin
	 *            start position
	 * @param length
	 *            count of instances
	 * @return Comment instances
	 */
	public List<Comment> findAll(int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		try {
			CommentDAO dao = new CommentDAO();
			List<Comment> result = dao.findAll(begin, length);
			return result;

		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	/**
	 * Get Comment instance by id
	 * 
	 * @param id
	 *            instance id
	 * @return Comment instance
	 */
	public Comment findById(java.lang.Integer id) {
		Session session = HibernateSessionFactory.getSession();
		try {
			CommentDAO dao = new CommentDAO();
			Comment result = dao.findById(id);
			return result;

		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	/**
	 * Find instances of Comment by a property value
	 * 
	 * @param propertyName
	 *            property name
	 * @param value
	 *            property value
	 * @return Comment instances
	 */
	public List<Comment> findByProperty(String propertyName, Object value) {
		Session session = HibernateSessionFactory.getSession();
		try {
			CommentDAO dao = new CommentDAO();
			List<Comment> result = dao.findByProperty(propertyName, value);
			return result;

		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}
	public List<Comment> findHiddenByProperty(String propertyName, Object value) {
		Session session = HibernateSessionFactory.getSession();
		try {
			CommentDAO dao = new CommentDAO();
			List<Comment> result = dao.findHiddenByProperty(propertyName, value);
			return result;

		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}
	/**
	 * Find instances of Comment by a property value
	 * 
	 * @param propertyName
	 *            property name
	 * @param value
	 *            property value
	 * @param begin
	 *            start position
	 * @param length
	 *            count of instances
	 * @return Comment instances
	 */
	public List<Comment> findByProperty(String propertyName, Object value,
			int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		try {
			CommentDAO dao = new CommentDAO();
			List<Comment> result = dao.findByProperty(propertyName, value,
					begin, length);
			return result;

		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}
	public List<Comment> findHiddenByProperty(String propertyName, Object value,
			int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		try {
			CommentDAO dao = new CommentDAO();
			List<Comment> result = dao.findHiddenByProperty(propertyName, value,
					begin, length);
			return result;

		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}
	@SuppressWarnings("unchecked")
	public List<Comment> findPublicCommentsByProperty(String propertyName, Object value,
			int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		try {
			CommentDAO dao = new CommentDAO();
			List<Comment> result = dao.findPublicCommentsByProperty(propertyName, value,
					begin, length);
			return result;

		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}
	/**
	 * Find the last comment of a message
	 * 
	 * @param messageID
	 *            message id
	 * @return the last comment of a message
	 */
	public Comment findLastComment(Integer gmid) {
		Session session = HibernateSessionFactory.getSession();
		try {
			CommentDAO dao = new CommentDAO();
			Comment result = dao.findLastComment(gmid);
			return result;

		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	/**
	 * Find the count of Attachment instances by a property value
	 * 
	 * @param propertyName
	 *            property name
	 * @param value
	 *            property value
	 * @return the count of Attachment instances
	 */
	public long findSizeByProperty(String propertyName, Object value) {
		Session session = HibernateSessionFactory.getSession();
		try {
			CommentDAO dao = new CommentDAO();
			long result = dao.findSizeByProperty(propertyName, value);
			return result;

		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}
 
	public long findHiddenSizeByProperty(String propertyName, Object value) {
		Session session = HibernateSessionFactory.getSession();
		try {
			CommentDAO dao = new CommentDAO();
			long result = dao.findHiddenSizeByProperty(propertyName, value);
			return result;

		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}
	/**
	 * Find the count of Comment instances by a property value
	 * 
	 * @param propertyName
	 *            property name
	 * @param value
	 *            property value
	 * @return the count of Attachment instances
	 */
	public long findPublicCommentsSizeByProperty(String propertyName, Object value) {
		Session session = HibernateSessionFactory.getSession();
		try {
			CommentDAO dao = new CommentDAO();
			long result = dao.findPublicCommentsSizeByProperty(propertyName, value);
			return result;

		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}
	/**
	 * Find the count of comments in a group
	 * 
	 * @param groupID
	 * @return the count of comments in a group
	 */
	public long searchSizeByGid(Integer groupID) {
		Session session = HibernateSessionFactory.getSession();
		try {
			CommentDAO dao = new CommentDAO();
			long result = dao.searchSizeByGid(groupID);
			return result;

		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	/**
	 * Find the count of comments during the last dayCount days
	 * 
	 * @param groupID
	 *            group id
	 * @param dayCount
	 *            the count of days
	 * @return the count of comments during the last dayCount days
	 */
	public long searchSizeByGidAndDays(Integer groupID, Integer dayCount) {
		Session session = HibernateSessionFactory.getSession();
		try {
			CommentDAO dao = new CommentDAO();
			long result = dao.searchSizeByGidAndDays(groupID, dayCount);
			return result;

		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

}